
package com.bds.tjj.action;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.bds.tjj.model.common.UserSession;

/**
 * @Title: BaseCtrl.java
 * @Package com.common.action
 * @author robin
 * @version V1.0
 */
public abstract class BaseAction {
	private static Logger logger = Logger.getLogger(BaseAction.class);

	/**
	 * 得到ModelAndView
	 */
	public ModelAndView getModelAndView() {
		return new ModelAndView();
	}

	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();

		return request;
	}

	public UserSession getUserSession(HttpServletRequest request) {
		UserSession us = null;
		Object session = request.getSession().getAttribute(UserSession.sessionKey);
		if (session != null) {
			us = (UserSession) session;
			if (!us.getType().equals("admin")) {
				Date startDate = us.getStart_date();
				Calendar cal = Calendar.getInstance();

				cal.setTime(startDate);

				cal.add(Calendar.DATE, us.getDay());

				Date endDate = cal.getTime();

				Date nowDate = new Date();
				if (nowDate.after(endDate)) {
					return null;

				}

			}

			return us;
		} else
			return null;

	}
}
